#include "../_includes.h"
#include "screen.h"

Display_Page cur_page = Display_Page::sensor_info; //包含常规页面和全屏
Display_Page last_normal_page = cur_page; //只包含常规页面，不保留全屏

bool display_cleared = true;
bool is_full_screen = false;

bool full_screen_stop_displaying = true;
bool full_screen_using = false;
bool page_manager_stop_displaying = false;
bool page_manager_using = false;

void clear_display_area() {
  screen_store_pen_font();
  screen_set_pen_font(fontType::mono20);

  for (int line_y = 0; line_y <= LCD_MAX_Y; line_y += 10)
    Print(0, line_y, "                         ");
  display_cleared = true;
  screen_restore_pen_font();
}

void page_up() {
  page_manager_stop_displaying = true; //叫停显示管理员
  full_screen_stop_displaying = true;  //叫停全屏显示线程
  while_wait(page_manager_using);
  while_wait(full_screen_using);
  screen_clear();
  buttons_show_up();

  if (cur_page == Display_Page::auto_full_screen)
    reset_disp_ui(); //清屏并恢复按钮
  else
    clear_display_area(); //仅仅清除显示区域

  int p;

  if (cur_page == Display_Page::auto_full_screen)
    p = (int)last_normal_page;
  else
    p = (int)cur_page;

  p++;
  p %= (int)Display_Page::max_info;

  if (cur_page != Display_Page::auto_full_screen)
    last_normal_page = cur_page;
  cur_page = (Display_Page)p;
  page_manager_stop_displaying = false; //允许管理员显示
  full_screen_stop_displaying = true;   //不允许全屏显示
}

void page_down() {
  page_manager_stop_displaying = true; //叫停显示管理员
  full_screen_stop_displaying = true;  //叫停全屏显示线程
  while_wait(page_manager_using);
  while_wait(full_screen_using);
  screen_clear();
  buttons_show_up();

  if (cur_page == Display_Page::auto_full_screen)
    reset_disp_ui(); //清屏并恢复按钮
  else
    clear_display_area(); //仅仅清除显示区域

  int p;
  if (cur_page == Display_Page::auto_full_screen)
    p = (int)last_normal_page;
  else
    p = (int)cur_page;
  p--;
  if (p == -1)
    p = (int)Display_Page::max_info - 1;

  if (cur_page != Display_Page::auto_full_screen)
    last_normal_page = cur_page;
  cur_page = (Display_Page)p;

  page_manager_stop_displaying = false; //允许管理员显示
  full_screen_stop_displaying = true;   //不允许全屏显示
}
///////////////////////////////////////////////////
void reset_disp_ui() {
  wait(10);
  screen_clear();
  buttons_show_up();
  is_full_screen = false;
}

void disp_clear_full_screen() {
  screen_clear();
  is_full_screen = true;
}

void enter_full_screen_once() {
  if (is_full_screen == false) { //非全屏
    //叫停显示主线程
    page_manager_stop_displaying = true;
    while_wait(page_manager_using || !screen_ready);

    //清屏，供全屏显示
    disp_clear_full_screen();
    buttons_show_hide(); //隐藏按钮，防止按压后有反应
    cur_page = Display_Page::auto_full_screen;
    full_screen_stop_displaying = false; //全屏可以显示了
  }
}

void exit_full_screen_once() {
  if (is_full_screen == true) {
    //叫停全屏显示线程
    full_screen_stop_displaying = true;
    while_wait(full_screen_using);
    //恢复界面
    reset_disp_ui();
    page_manager_stop_displaying = false; //管理员可以显示了
    cur_page = last_normal_page;          //恢复常规页面
  }
}

void ask_for_full_screen() { //申请全屏
  if (is_full_screen == false) {
    thread t(enter_full_screen_once);
    wait(10);
  }
}

void exit_full_screen() { //退出全屏
  if (is_full_screen == true) {
    thread t(exit_full_screen_once);
    wait(10);
  }
}
///////////////////////////////////////////////////////////////////////////
bool screen_ready = false;
void display_manager_thread_routine() {
  //////////////////
  extern void team_num_loading();
  extern void code_drop();

  thread t1(team_num_loading); //队号加载
  thread t2(code_drop);        //代码雨背景
  while (t1.joinable()) {
    if (Brain.Screen.pressing() || special_key.pressing())
      t1.interrupt();
    wait(50);
  }
  t2.interrupt();
  wait(100);
  ///////////////////////////////////////////
  screen_set_fill_color(black);
  screen_clear();
  buttons_init();
  screen_ready = true; //屏幕初始化完成
  while (1) {
    //显示的主线程要做的事情
    if (!page_manager_stop_displaying &&
        cur_page != Display_Page::auto_full_screen) {
      page_manager_using = true;
      auto_start_btn_func();
      imu_recalibrate_func();
      auto_start_btn_func();
      imu_recalibrate_func();
      if (cur_page == Display_Page::sensor_info)
        disp_page_sensors();
      if (cur_page == Display_Page::motor_port_info)
        disp_page_ports_motor();
      if (cur_page == Display_Page::sensor_port_info)
        disp_page_ports_sensor();
      if (cur_page == Display_Page::controller_axis_info)
        disp_page_axis();
      page_manager_using = false;
    } else {
      //外界有全屏或其他显示的需求
      wait(50); //直接切换线程
    }

    wait(100);
    // pause updating when autonomous_running
    if (autonomous_running) {
      while (autonomous_running)
        wait(200);
      wait(3500); // wait for all auto display thread done
      screen_set_pen_font(fontType::mono20);
      screen_clear();
      buttons_show_up();
    }
  }
}